home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / modal-dialog-sample ƒ / Dlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  3.4 KB  |  196 lines  |  [TEXT/KAHL]

  1. /*
  2. **    Project    dialog example
  3. **    Module    Dlog.c
  4. **    Author    Bernie Wieser ©1991
  5. **    Date    10/26/91
  6. **
  7. **    Purpose
  8. **        An example program showing how to handle default keyboard
  9. **    input, correct update re-drawing of the dialog, and input
  10. **    filtering.
  11. **        This code is for educational purpose only.  Any use thereof
  12. **    requires appropriate credit.
  13. **
  14. **    Bugs
  15. **        No support for keyboard te facilities.
  16. */
  17. #include "Dlog.h"
  18. #include "common.h"
  19.  
  20. static pascal Boolean filter(
  21.     DialogPtr    dp,
  22.     EventRecord    *er,
  23.     short        *itemHit
  24.     )
  25. {
  26.     Byte    c;
  27.     Boolean status;
  28.     short    err;
  29.     
  30.     status = FALSE;
  31.     
  32.     SetPort(dp);
  33.     if(er->what == updateEvt) {
  34.         /*
  35.         ** don't call beginUpdate and endUpdate,
  36.         ** since this will mess up DrawDialog
  37.         */
  38.         err = DefaultButton(dp, BOKAY);
  39.         }
  40.     if(er->what == keyDown) {
  41.         c = er->message & charCodeMask;
  42.         /*
  43.         **    Is it a command key?
  44.         */
  45.         if(er->modifiers & cmdKey) switch(c) {
  46.             /* Cancel cmd-. */
  47.             case '.':
  48.                 *itemHit = BCANCEL;
  49.                 status = TRUE;
  50.                 err = PressButton(dp, BCANCEL, 6L);
  51.                 break;
  52.             /* Save cmd-s */
  53.             case 's':
  54.             case 'S':
  55.                 *itemHit = BSAVE;
  56.                 status = TRUE;
  57.                 err = PressButton(dp, BSAVE, 6L);
  58.                 break;
  59.             default:
  60.                 break;
  61.             }
  62.         else switch(c) {
  63.             /*
  64.             **    Edit keys
  65.             */
  66.             case 0x8:
  67.             case 0x9:
  68.             case 0x1c:
  69.             case 0x1d:
  70.             case 0x1e:
  71.             case 0x1f:
  72.                 break;
  73.             /*
  74.             **    Default key, return
  75.             */
  76.             case 0xD:
  77.                 *itemHit = BOKAY;
  78.                 status = TRUE;
  79.                 err = PressButton(dp, BOKAY, 6L);
  80.                 break;
  81.             /*
  82.             **    Escape key, cancel
  83.             */
  84.             case 0x1b:
  85.                 *itemHit = BCANCEL;
  86.                 status = TRUE;
  87.                 err = PressButton(dp, BCANCEL, 6L);
  88.                 break;
  89.             /*
  90.             **    Other keys are context specific
  91.             */
  92.             default:
  93.                 switch(((DialogPeek)dp)->editField+1) {
  94.                     case ETEXTFLD:
  95.                         if(!isletter(c)) {
  96.                             /* not a letter?  eat it */
  97.                             *itemHit = 0;
  98.                             status = TRUE;
  99.                             }
  100.                         break;
  101.                     case ENUMFLD:
  102.                         if(!isnumber(c)) {
  103.                             /* not a number?  eat it */
  104.                             *itemHit = 0;
  105.                             status = TRUE;
  106.                             }
  107.                         break;
  108.                     default:
  109.                         break;
  110.                     }
  111.                 break;
  112.             }
  113.         }
  114.     return(status);
  115. }
  116.  
  117. void GetDHandle(
  118.     DialogPtr    dp,
  119.     short        item,
  120.     Handle        *handy
  121.     )
  122. {
  123.     Rect    box;
  124.     short    type;
  125.     
  126.     GetDItem(dp, item, &type, handy, &box);
  127. }
  128.  
  129. void DialogTest(
  130.     short    dlogid
  131.     )
  132. {
  133.     DialogRecord    dr;
  134.     DialogPtr        dp;
  135.     short            x,
  136.                     err;
  137.     pMyData            pmd;
  138.     short            item;
  139.     Boolean            done;
  140.     
  141.     /*
  142.     **    Set up the dialog
  143.     */
  144.     dp = GetNewDialog(dlogid, &dr, (WindowPtr)-1);
  145.     if(err = ResError())
  146.         DebugStr("\pGetNewDialog failed");
  147.     
  148.     /*
  149.     **    Set up a record of important item handles for
  150.     **    easy reference by all routines i.e. setup, filters, etc.
  151.     **    Dangle it of the dlog's window's refCon, since this
  152.     **    data will always be available to every routine!
  153.     **    (Not used in this example.)
  154.     */
  155.     pmd = (pMyData)NewPtr((long)sizeof(myData));
  156.     if(err = MemError())
  157.         DebugStr("\ppmd NewPtr failed");
  158.     for(x=0; x<END; x++) {
  159.         GetDHandle(dp, x+1, &pmd->harray[x]);
  160.         }
  161.     ((DialogPeek)dp)->window.refCon = (long)pmd;
  162.         
  163.     /*
  164.     **    Handle the dialog
  165.     */
  166.     done = FALSE;
  167.     while(!done){
  168.         ModalDialog(filter, &item);
  169.         switch(item) {
  170.             /*
  171.             **    Handle the Buttons
  172.             */
  173.             case BOKAY:    /* done, don't save */
  174.                 done = TRUE;
  175.                 break;
  176.             case BCANCEL:
  177.                 done = TRUE;
  178.                 break;
  179.             case BSAVE:    /* save default */
  180.                 done = TRUE;
  181.                 break;
  182.             /*
  183.             **    Handle mouse downs in text field!  Isn't
  184.             **    too useful, so nothing is done here.
  185.             */
  186.             case ETEXTFLD:
  187.                 break;
  188.             case ENUMFLD:
  189.                 break;
  190.             default:
  191.                 break;
  192.             }
  193.         }
  194.     DisposPtr(pmd);
  195.     CloseDialog(dp);
  196. }